/**
 * @param {string} s
 * @return {number}
 */
var myAtoi = function (s) {
  let start = -1;
  let end = -1;
  for (let i = 0; i < s.length; i++) {
    let ch = s.charCodeAt(i);
    if (ch === 32) {
      if (start === -1) {
        continue;
      } else {
        break;
      }
    }
    if (ch === 46 || (ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122)) {
      break;
    }
    if (ch === 43 || ch === 45) {
      if (start !== -1) {
        break;
      }
      start = i;
      continue;
    }
    if (start === -1) {
      start = i;
    }
    end = i;
  }
  if (start === -1 || end === -1) {
    return 0;
  }
  let num = Number(s.substring(start, end + 1));
  if (num > 2147483647) {
    num = 2147483647;
  } else if (num < -2147483648) {
    num = -2147483648;
  }
  return num;
};

